const {
  getLbt,
  getFenlei,
  getRmhd,
  getShopList,
} = require("../../api/homeApi");

Page({
  /**
   * 页面的初始数据
   */
  data: {
    lbt: [], // 轮播图
    categroy1: [], // 分类1
    categroy2: [], // 分类2
    rmhd: [], // 热门活动
    shopList: [], // 商品列表
    token: "",
    location: [],
    shi: "济南",
  },

  // 获取轮播图
  getLbtHandle() {
    const data = {
      token: "",
      type: 1,
    };

    const token = wx.getStorageSync("token");

    data.token = token;

    getLbt(data)
      .then((res) => {
        this.setData({
          lbt: res.data.data.banner,
        });
      })
      .catch((err) => {
        console.log("err-->", err);
      });
  },

  // 获取分类
  async getFenleiHandle() {
    const res = await getFenlei();
    this.setData({
      categroy1: res.data.data.list.slice(0, 4),
      categroy2: res.data.data.list.slice(3, 7),
    });
  },

  // 获取热门活动
  async getRmhdHandle() {
    const res = await getRmhd({ page: 1, pageSize: 10 });
    this.setData({
      rmhd: res.data.data.list,
    });
  },

  // 获取商品列表
  async getShopListHandle() {
    const res = await getShopList();
    this.setData({
      shopList: res.data.data.list,
    });
  },

  // 商品详情跳转
  shopDetail(e) {
    let shopId = e.currentTarget.dataset.shopdetail.id;
    wx.navigateTo({
      url: "../serviceDetails/serviceDetails?id=" + shopId,
    });
  },

  // 下单
  async placeAnOrderHandle() {
    const placeAnOrderData = {
      token: this.data.token,
      goodsid: "55",
      activityid: "1",
      ordertype: "1",
      orderposition: "1",
      coupon_id: "0",
      score: "0",
      discount: "0",
      paid_price: "99",
      pre_book_begintime: "0",
      pre_book_endtime: "0",
      bak: "",
      address_id: "1",
      workerid: "1",
      unitid: "0",
      count: "1",
    };
    const res = await placeAnOrder(placeAnOrderData);
    console.log(res.data.data.order);
  },

  // 去选位置
  toLocation() {
    wx.navigateTo({
      url: "../location/location",
    });
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    console.log("传参--->", options);
    const token = wx.getStorageSync("token");
    if (!token) {
      wx.navigateTo({
        url: "../login/login",
      });
    } else {
      this.setData({
        token,
      });
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {},

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    this.getLbtHandle();
    this.getFenleiHandle();
    this.getRmhdHandle();
    this.getShopListHandle();
    // this.placeAnOrderHandle();
    const dizhi = wx.getStorageSync("dizhi");
    console.log(dizhi);
    this.setData({
      shi:dizhi.shi
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {},

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {},

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {},

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {},

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {},
});
